﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AutoParsers;
using DL;

public partial class MasterPage : System.Web.UI.MasterPage
{
    CarService carservice = new CarService();


    protected void Page_Init(object sender, EventArgs e)
    {
        if (Session["AuthUser"] == null)
        {
            EnterSite.Visible = true;
            MailandLogOut.Visible = false;
        }
        else
        {
            EnterSite.Visible = false;
            MailandLogOut.Visible = true;
            var user = (Session["AuthUser"] as User);
            userNameBox.InnerText = user.UserName;
        }
    }

    public User GetAuthUser()
    {
        var user = (Session["AuthUser"] as User);
        return user;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDate();
        }
    }

    private void BindDate()
    {
        ddlCategory.DataSource = carservice.GetCategories();
        ddlCategory.DataBind();
        ddlFuelType.DataSource = carservice.GetFuelTypes();
        ddlFuelType.DataBind();
        ddlColor.DataSource = carservice.GetColors();
        ddlColor.DataBind();
        ddlManufacture.DataSource = carservice.GetManufacturers();
        ddlManufacture.DataBind();
        if (ddlManufacture.SelectedValue != null)
        {
            ddlModel.DataSource = carservice.GEtModelByManufacturer(Convert.ToInt32(ddlManufacture.SelectedValue));
            ddlModel.DataBind();
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        using (var dbServ = new UserService())
        {
            User visitingUser;
            if (dbServ.Authenticate(userName.Value, password.Value, out visitingUser))
            {
                Session["AuthUser"] = visitingUser;
                Response.Redirect("~/");
            }
            else
            {
                Script.Text = "<script>alert('ავტორიზაცია ვერ გაიარეთ'); window.location('/default.aspx');</script>";
            }
        }
    }

    protected void Add_Click(object sender, EventArgs e)
    {
        AutoParsers.ParseService service = new AutoParsers.ParseService();

        ParsedCar parsedCar = service.ParseCar(linkInput.Value);

        SaveCar(parsedCar);

        Response.Redirect("/Default.aspx");
    }


    public void SaveCar(ParsedCar mycar)
    {

        Car car = new Car();
        car.ModelID = carservice.GetModelByName(mycar.Model);
        car.ColorID = carservice.GetColorByName(mycar.Color);
        car.FuelTypeID = carservice.GetFuelTypeByName(mycar.FuelType);
        car.CategoryID = carservice.GetCategoryByName(mycar.Category);

        car.GPSNavigation = mycar.GPSNavigation;
        car.Heated = mycar.Heated;
        car.Hydraulics = mycar.Hydraulics;
        car.IsDeleted = false;
        car.IsSteeringRight = mycar.IsSteeringRight;
        car.LeatherInterior = mycar.LeatherInterior;
        car.LightSensor = mycar.LightSensor;
        car.LightsTreatment = mycar.LightsTreatment;
        car.LoadCarryingCapacity = mycar.LoadCarryingCapacity;
        car.LocationID = -1;//mycar.Location;
        car.Mileage = mycar.Mileage;
        car.MirrorsWarm = mycar.MirrorsWarm;
        car.Parking = mycar.Parking;
        car.Price = mycar.Price;
        car.RainSensor = mycar.RainSensor;
        car.SealingWax = mycar.SealingWax;
        car.StatusID = 1;/*new*/
        car.SteeringWheelAdjust = mycar.SteeringWheelAdjust;
        car.Transmission = mycar.Transmission;
        car.URL = mycar.URL == null ? " " : mycar.URL;
        car.UserID = -1;//currentUserID;
        car.Year = mycar.Year;
        car.Condition = mycar.Condition;
        car.ABS = mycar.ABS;

        /*Get owner*/
        DL.Owner owner = new DL.Owner();
        owner.FullName = mycar.Owner.FullName;
        owner.Mobile = mycar.Owner.Mobile;
        owner.Email = mycar.Owner.Email;
        owner.SkypeName = mycar.Owner.SkypeName;
        /*/Get owner*/
        car.CreateDate = DateTime.Today;

        carservice.SaveCar(car, owner, mycar.Images);
    }

    public void addButton_ServerClick(object sender, EventArgs e)
    {
        ParseService parseService = new ParseService();
        string link = linkInput.Value;
        var item = parseService.ParseCar(link);
        SaveCar(item);
    }
    protected void lbAdd_Click(object sender, EventArgs e)
    {
        Car mycar = new Car();

        mycar.ManufacturerID = Convert.ToInt32(ddlManufacture.SelectedValue);
        mycar.ModelID = Convert.ToInt32(ddlModel.SelectedValue);
        mycar.ColorID = Convert.ToInt32(ddlColor.SelectedValue);
        mycar.CategoryID = Convert.ToInt32(ddlCategory.SelectedValue);
        mycar.StatusID = 1;/*new*/
        mycar.FuelTypeID = Convert.ToInt32(ddlFuelType.SelectedValue);
        mycar.IsSteeringRight = Convert.ToBoolean(ddlWeel.SelectedIndex);
        mycar.CreateDate = DateTime.Today;
        mycar.Engine = txtDzravi.Value;
        mycar.Cylinders = Convert.ToInt32(txtCilinder.Value);
        mycar.Price = txtPrice.Value;
        mycar.Year = txtProduceYear.Value;
        mycar.Transmission = txtTransmission.Value;
        mycar.Doors = txtDoors.Value;
        mycar.Transmission = txtTransmission.Value;
        mycar.ElWindow = chElWin.Checked;


        //carservice.SaveCar
    }
}
